<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户登录</title>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>

    <link rel="stylesheet" href="../js/libs/sui/sm.min.css">
    <link rel="stylesheet" href="../js/libs/sui/sm-extend.min.css">
    <link rel="stylesheet" href="../iconfont/iconfont.css">
    <link rel="stylesheet" href="../less/main.css"/>

    <script type="text/javascript" src='../js/libs/jquery-3.1.0.min.js' charset='utf-8'></script>
    <script type="text/javascript">
        var Zepto = jQuery;
    </script>
    <script type="text/javascript" src='../js/libs/sui/sm.js' charset='utf-8'></script>
    <script type="text/javascript" src='../js/libs/sui/sm-extend.min.js' charset='utf-8'></script>
    <script type="text/javascript" src="../js/libs/juicer-min.js"></script>
    <script type="text/javascript" src="../js/libs/jquery.md5.js"></script>
    <script type="text/javascript" src="../js/api.js"></script>
    <script type="text/javascript" src="../js/utils.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
<div class="content user-login bg-default p-t22">
    <!--头部-->
    <div class="head-no-toolbar bg-self-green no-left-icon">
        <span class="icon iconfont c-white p-l" onclick="javascript:api.closeWin();">&#xe61f;</span>
        <span class="c-white t-center">用户登录</span>
    </div>
    <div class="list-block">
        <ul>
            <li>
                <div class="item-content">
                    <div class="item-media"><i class="icon iconfont">&#xe619;</i></div>
                    <div class="item-inner">
                        <div class="item-input">
                            <input id="user-name" type="text" placeholder="输入手机号">
                        </div>
                    </div>
                </div>
            </li>
            <li>
                <div class="item-content">
                    <div class="item-media"><i class="icon iconfont">&#xe61e;</i></div>
                    <div class="item-inner">
                        <div class="item-input">
                            <input id="user-pwd" type="password" placeholder="输入密码">
                        </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <div class="content-block">
        <div class="row">
            <div class="col-100">
                <a id="login-btn"  class="button button-big button-round button-fill bg-self-green">登录</a>
            </div>
        </div>
    </div>
    <div class="enroll">
        <a id="goto-register-btn" onclick="javascript:openWin('user-enroll', 'user-enroll.html', null)" progress="false">新用户注册
            <span class="icon iconfont">&#xe620;</span>
        </a>
    </div>
</div>
<script type="text/javascript">
    apiready = function () {
        juicer.set({
            'tag::operationOpen': '{@',
            'tag::operationClose': '}',
            'tag::interpolateOpen': '&{',
            'tag::interpolateClose': '}',
            'tag::noneencodeOpen': '$${',
            'tag::noneencodeClose': '}',
            'tag::commentOpen': '{#',
            'tag::commentClose': '}',
            'strip': false,
            'cache': false
        });


        //接收登陆成功事件并初始化账号
        api.addEventListener({
            name: 'regSuccessEvent'
        }, function (ret, err) {
            $("#user-name").val(ret.value.phone);
        });

        //登陆
        $("#login-btn").click(function() {
            //1500140001
            var loginSql = "SELECT * FROM SU_STAFF T WHERE T.STAFF_NO='{0}'";
            var stuSql = "SELECT * FROM S_STUDENT T WHERE T.STAFF_ID='{0}'";
            // 用户名
            var username = $("#user-name").val();
            // 密码
            var userpwd = $("#user-pwd").val();

            if (isEmpty(username)) {
                toast("请输入账号");

                return;
            }

            if (isEmpty(userpwd)) {
                toast("请输入密码");

                return;
            }

            var paramObj = api.pageParam;
            dbQuery(1, 1, loginSql.format(username), true, function (ret, err) {
                if (isObject(ret) && (ret.statusCode == 0)) {
                    var userInfo = ret.result[0];
                    if (isObject(userInfo)) {
                        if ($.md5(userpwd) === userInfo.PWD) {
                            dbQuery(1, 1, stuSql.format(userInfo.STAFF_ID), true, function (ret, err) {
                                if (isObject(ret) &&  (ret.statusCode == 0)) {
                                    var studentInfo = ret.result[0];

                                    if (isObject(studentInfo)) {
                                        // 保存登录信息
                                        setStaffInfo("loginUserInfo", userInfo);
                                        // 保存学生数据
                                        setStaffInfo("loginStudentInfo", studentInfo);
                                        // 保存登录标志为已登录
                                        setStaffInfo("loginFlag", "true");

                                        toast("登陆成功");

                                        // 发送登录事件
                                        api.sendEvent({
                                            name : "loginEvent",
                                            extra : {
                                                flag : paramObj.flag
                                            }
                                        });

                                        // 关闭登录窗口
                                        closeWin();

                                    } else {
                                        logout();

                                        toast("登陆失败，没有学生数据");
                                    }
                                } else {
                                    logout();

                                    toast("登录失败，获取学生数据失败");
                                }
                            })

                        } else {
                            logout();

                            toast("登录失败，密码错误");
                        }
                    } else {
                        logout();

                        toast("登录失败，账号错误");
                    }
                } else {
                    logout();
                }
            });
        });

    };


</script>
</body>
</html>